from flask import Blueprint, request, session
from flask_restful import Api, Resource, marshal_with
from flask_restful.reqparse import RequestParser

from webapp.config import api_utils
from webapp.config.api_utils import oper_log, check_auth
from webapp.dto_mapper import grade_page_fields
from webapp.schema import GradeSchema
from webapp.service.grade_service import GradeService

grade_app = Blueprint('grade', __name__, url_prefix='/api/grade')
grade_api = Api(grade_app)

parser = RequestParser()
parser.add_argument('id', location='json', type=int, trim=True, required=True)
parser.add_argument('majorname', location='json', type=str, trim=True, required=True)
parser.add_argument('majorcode', location='json', type=str, trim=True, required=True)
parser.add_argument('classname', location='json', type=str, trim=False, required=False)


@grade_api.resource("", "/")
class GradeApi(Resource):

    @oper_log('查询所有班级', request)
    @check_auth('grade:list', request)
    @marshal_with(grade_page_fields)
    def get(self):
        params = request.args.items(multi=True)
        params_dict = api_utils.build_params(params)
        return GradeService.query_all(**params_dict)

    @oper_log('删除班级', request)
    @check_auth('grade:del', request)
    def delete(self):
        ids = request.get_json()
        grade_set = set()
        for _id in ids:
            grade_set.add(GradeService.find_by_id(id=_id))
        GradeService.delete(grade_set=grade_set)

    @oper_log('新增班级', request)
    @check_auth('grade:add', request)
    def post(self):
        parser_post = parser.copy()
        parser_post.remove_argument('id')
        parser_post.parse_args()
        params = request.get_json()
        schema = GradeSchema()
        curr_grade = schema.load(params, unknown="exclude")
        user = session["user"]
        curr_grade.create_by = user["username"]
        GradeService.create(curr_grade=curr_grade)

    @oper_log('修改班级', request)
    @check_auth('grade:edit', request)
    def put(self):
        parser_put = parser.copy()
        parser_put.parse_args()
        params = request.get_json()
        schema = GradeSchema()
        curr_grade = schema.load(params, unknown="exclude")
        user = session["user"]
        curr_grade.update_by = user["username"]
        GradeService.update(curr_grade=curr_grade)


@grade_api.resource("", "/week/<int:id>")
class GradeWeekApi(Resource):
    @oper_log('设置班级可上课周次', request)
    @check_auth('grade:edit', request)
    def post(self, id):
        studyweek = request.json.get('studyweek')
        studyweekstr = ','.join(str(i) for i in studyweek)
        user = session["user"]
        update_by = user["username"]
        GradeService.updateStudyWeek(id, update_by,studyweekstr)

    @oper_log('查询当前已经设置的周次', request)
    @check_auth('grade:list', request)
    def get(self, id):
        grade = GradeService.find_by_id(id)
        result = grade.studyweek
        if result is None:
            result = ""
        return result